package edu.thu.thss.rsa.test;

//RSATestFast: Test Fast RSA Implementation
import java.math.*; // for BigInteger
import java.util.*; // for Random
public class RSATestFast {
 
   public static void elapsedTime(long startTime) {
      long stopTime = System.currentTimeMillis();
      double elapsedTime = ((double)(stopTime - startTime))/1000.0;
      System.out.println("Elapsed time: " + elapsedTime + " seconds");
   }

   public static void main(String[] args) {
      Random rnd = new Random();
      BigInteger m, m1, m2, m3, c, s, s1;
      RSAPrivateKeyFast alice = new RSAPrivateKeyFast(1024, rnd, "Alice");
      RSAPrivateKeyFast bob   = new RSAPrivateKeyFast(1024, rnd, "Bob  ");
      m = new BigInteger(
        "1234567890987654321012345678909876543210" + 
        "1234567890987654321012345678909876543210" +
        "1234567890987654321012345678909876543210" + 
        "1234567890987654321012345678909876543210" +
        "1234567890987654321012345678909876543210" + 
        "1234567890987654321012345678909876543210");
      System.out.println("Message m:\n" + m + "\n");
      System.out.println("ALICE ENCRYPTS m FOR BOB; BOB DECRYPTS IT:");
      c = bob.RSAEncrypt(m); // Using Bob's public key
      System.out.println("Message encrypted with Bob's public key:\n" +
         c + "\n");
      m1 = bob.RSADecrypt(c); // Using Bob's private key
      System.out.println("Original message back, decrypted:\n" + m1 + "\n");

      System.out.println("ALICE SIGNS m FOR BOB; BOB VERIFIES SIGNATURE:");
      s = alice.RSASign(m); // Using Alice's private key
      System.out.println("Message signed with Alice's private key:\n" +
         s + "\n");
      m2 = alice.RSAVerify(s); // Using Alice's public key
      System.out.println("Original message back, verified:\n" + m2 + "\n");

      System.out.println("BOB SIGNS AND ENCRYPTS m FOR ALICE;" +
         "\n   ALICE VERIFIES SIGNATURE AND DECRYPTS:");
      c  = bob.RSASignAndEncrypt(m, alice);
      System.out.println("Message signed and encrypted," +
         "\n  using Bob's secret key and Alice's public key:\n" + c + "\n");
      m3 = alice.RSADecryptAndVerify(c, bob);
      System.out.println("Original message back, verified and decrypted," +
         "\n  using Alice's secret key and Bob's public key:\n" + m1); 
   }
}
